package com.lei.study.mvel.def;

import org.junit.Test;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolverFactory;
import org.mvel2.util.ParseTools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 伍磊
 */
public class BasicFunction {

    private Map<String, Object> map = new HashMap<>(16);

    @Test
    public void test() throws IOException {
        File file = new File("/Users/wulei/code-personal/mvel/mvel-study/src/main/resources/mvel/test.mvel");
        InputStream inputStream = new FileInputStream(file);
        Serializable serializable = MVEL.compileExpression(new String(ParseTools.readIn(inputStream, null)));

        map.put("a", 1);
        map.put("b", 10);

        VariableResolverFactory resolverFactory = new MapVariableResolverFactory(map);
        Object o = MVEL.executeExpression(serializable, null, resolverFactory);
        System.out.println(o);
    }
}
